//
//  MCBasicViewController+NavigationBar.m
//  MCPublicModule
//
//  Created by Mirko on 15/12/29.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "MCBasicViewController+NavigationBar.h"
#import "MCUIButton.h"

const static NSInteger kDefaultGap = 20;

@implementation MCBasicViewController (NavigationBar)

-(void) setUpNavigationArea
{
    [self addLeftArea];
    
    [self addCenterArea];

    [self addRightArea];
}

#pragma mark set&get menthod
-(NSInteger) leftAreaW
{
    return ScreenWidth() / 3 - 2 * kDefaultGap;
}

-(NSInteger) centerAreaW
{
    return self.leftAreaW + 2 * kDefaultGap;
}

-(NSInteger) rightAreaW
{
    return self.leftAreaW;
}


// 添加左区域
- (void)addLeftArea {
    self.leftArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.leftAreaW, kNavigationBarHeight)];
    self.leftArea.backgroundColor = RandomColor();
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:self.leftArea]];
    [self setUpMCNavigationBarButton];
}


// 添加中间区域
- (void)addCenterArea {
    
    self.centerArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.centerAreaW, kNavigationBarHeight)];
    self.centerArea.backgroundColor = RandomColor();
    [self.navigationItem setTitleView:self.centerArea];

    [self setUpMCNavigationBarButton];
}

// 添加右区域
- (void)addRightArea {
    
    self.rightArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.rightAreaW, kNavigationBarHeight)];
    self.rightArea.backgroundColor = RandomColor();
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:self.rightArea]];

    [self setUpMCNavigationBarButton];
}



-(void) setUpMCNavigationBarButton
{
    if (self.leftArea && !self.leftAreaLBtn) {
        
        MCUIButton *leftButton = [MCUIButton buttonWithType:UIButtonTypeCustom];
        leftButton.backgroundColor = RandomColor();
        leftButton.tag = kMCNavActionLeftAreaLeft;
        [leftButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.leftArea addSubview:leftButton];
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.leftArea.mas_left);
            make.width.mas_equalTo(@40);
            make.centerY.mas_equalTo(self.leftArea.mas_centerY);
            make.height.mas_equalTo(@30);
        }];
        self.leftAreaLBtn = leftButton;
    }
    
    if (self.centerArea && !self.titleButton) {
        
        MCUIButton *titleButton = [MCUIButton buttonWithType:UIButtonTypeCustom];
        titleButton.backgroundColor = RandomColor();
        titleButton.tag = kMCNavActionCenterTitle;
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.centerArea addSubview:titleButton];
        [titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(10, 10, 10, 10));
        }];
        self.titleButton = titleButton;
    }
    
    if (self.rightArea && !self.rightAreaRBtn) {
        
        MCUIButton *rightButton = [MCUIButton buttonWithType:UIButtonTypeCustom];
        rightButton.tag = kMCNavActionRightAreaRight;
        rightButton.backgroundColor = RandomColor();
        [rightButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.rightArea addSubview:rightButton];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.rightArea.mas_right);
            make.width.mas_equalTo(@40);
            make.centerY.mas_equalTo(self.rightArea.mas_centerY);
            make.height.mas_equalTo(@30);
        }];
        self.rightAreaRBtn = rightButton;
    }
    
   
}

-(void) addLeftAreaRightButton
{
    MCUIButton *rightButton = [MCUIButton buttonWithType:UIButtonTypeCustom];
    rightButton.backgroundColor = RandomColor();
    rightButton.tag = kMCNavActionLeftAreaRight;
    [rightButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.leftArea addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.leftArea.mas_right);
        make.width.mas_equalTo(@40);
        make.centerY.mas_equalTo(self.leftArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    self.leftAreaRBtn = rightButton;
}

-(void) addRightAreaLeftButton 
{
    MCUIButton *leftButton = [MCUIButton buttonWithType:UIButtonTypeCustom];
    leftButton.backgroundColor = RandomColor();
    leftButton.tag = kMCNavActionRightAreaLeft;
    [leftButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightArea addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rightArea.mas_left);
        make.width.mas_equalTo(@40);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    self.rightAreaLBtn = leftButton;
}


-(void) setupNavigationBar:(EMCNavgationBarType) type
{
    [self setUpNavigationArea];
    [self setUpMCNavigationBarButton];
    switch (type) {
        case kMCNavgationBarTypeCompoundLeft:
        {
            [self addLeftAreaRightButton];
        }
            break;
        case kMCNavgationBarTypeCompoundRight:
        {
            [self addRightAreaLeftButton];
        }
            break;
        case kMCNavgationBarTypeCompoundLeftAndRight:
        {
            [self addLeftAreaRightButton];
            [self addRightAreaLeftButton];
        }
            break;
        default:
            break;
    }
}

-(void) handleNavButtonAction:(MCUIButton *) button
{
    [self handleNavigationAction:(EMCNavigationActionType)button.tag];
}

@end
